package com.fighthouse.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.fighthouse.entity.Video;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.client.youtube.YouTubeQuery.OrderBy;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class VideoService {
	
	private String productName = "FightHouse";
	private String dev_id = "ytapi-AugustusCosta-YouTubeTest-f8hhkonl-1";
	private String dev_key = "AI39si5Oi0ceBpvnA4_c40wuaLZ6CRq9uWYvv_R-wThl4rtQA1Y-id_Eu62PeDtUTcMU5nYvVT4twL2rfP7dFoL0kVPTHlveUg";
	private YouTubeService service;
	private String youtubeUserPass = "luta2009";
	private String youtubeUserLogin = "fighthouse.fh@gmail.com";
	private String videosQuery = "http://gdata.youtube.com/feeds/api/users/default/uploads";
	private String testes = "http://gdata.youtube.com/feeds/api/standardfeeds/most_recent";
	private String coberturasQuery = "http://gdata.youtube.com/feeds/api/playlists/15AC9EADB70712C9";
	private String aulasQuery = "http://gdata.youtube.com/feeds/api/playlists/8D8B4CC5261E6002";
	
	public VideoService(){
		super();
	}
	public List<Video> getVideos()throws ServiceException,IOException,MalformedURLException{
		create();
		VideoFeed videoFeed;
		try{
			YouTubeQuery query = new YouTubeQuery(new URL(testes));
			query.setOrderBy(OrderBy.UPDATED);
			videoFeed = service.query(query,VideoFeed.class);
			return formatEntry(videoFeed);
		}catch(Exception e){
			System.out.println(e.getCause());
		}
		return null;
	}
	public List<Video> getAulas()throws ServiceException,IOException,MalformedURLException{
		create();
		VideoFeed videoFeed;
		try{
			YouTubeQuery query = new YouTubeQuery(new URL(testes));
			query.setOrderBy(OrderBy.UPDATED);
			videoFeed = service.query(query,VideoFeed.class);
			return formatEntry(videoFeed);
		}catch(Exception e){
			System.out.println(e.getCause());
		}
		return null;
	}
	public List<Video> getCoberturas()throws ServiceException,IOException,MalformedURLException{
		create();
		VideoFeed videoFeed;
		try{
			YouTubeQuery query = new YouTubeQuery(new URL(testes));
			query.setOrderBy(OrderBy.UPDATED);
			videoFeed = service.query(query,VideoFeed.class);
			return formatEntry(videoFeed);
		}catch(Exception e){
			System.out.println(e.getCause());
		}
		return null;
	}
	
	private void create() throws AuthenticationException{
		if(service == null){
			service = new YouTubeService(dev_id,dev_key);
			service.setUserCredentials(youtubeUserLogin, youtubeUserPass);
		}
	}
	private String giveID(String fullId){
		String toReturn = fullId.substring(fullId.lastIndexOf(":") + 1, fullId.length());
		return toReturn;
	}
	private List<Video> formatEntry(VideoFeed videoFeed){
		List<Video> toReturn = new ArrayList<Video>();
		for(VideoEntry entry : videoFeed.getEntries() ) {
			Video video = new Video();
			video.setId(giveID(entry.getId()));
			video.setTitulo(entry.getTitle().getPlainText());
			video.setDescricao(entry.getMediaGroup().getDescription().getPlainTextContent());
			if(entry.getMediaGroup().getContents().size() > 0) video.setPlayerUrl(entry.getMediaGroup().getContents().get(0).getUrl());
			if(entry.getMediaGroup().getThumbnails().size() > 0) video.setThumbUrl(entry.getMediaGroup().getThumbnails().get(0).getUrl());
			toReturn.add(video);
		}
		return toReturn;
	}
}
